<?php

namespace App\Console\Commands\Wanggui;

use App\Models\Common\ResourcesModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUploadFileResourceModel;
use Illuminate\Console\Command;
use Kuafu\File\FileUtil;

class TempUpdateLineMarkingFile extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'wanggui:temp-update-line-marking-file';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '临时修改动线标注文件脚本';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        OpenShopUploadFileResourceModel::query()
            ->where('position', 68)
            ->with(['openShopSiteLocationsRel'])
            ->chunkById(100, function ($models) {
                $data = [];
                $models->map(function (OpenShopUploadFileResourceModel $fileResourceModel) use (&$data) {
                    $data[] = [
                        'businessId'   => (int)optional($fileResourceModel->openShopSiteLocationsRel)->id,
                        'module'       => ResourcesModel::MODULE_TYPE_MOVING_LING_MARKING_DESC,
                        'business'     => ResourcesModel::BUSINESS_MOVING_LING_MARKING_DESC,
                        'name'         => 'lineMarking',
                        'url'          => $fileResourceModel->url,
                        'isPublic'     => 1,
                        'extra'        => json_encode([
                            'path' => $fileResourceModel->url,
                            'type' => 'image',
                            'url'  => FileUtil::domain() . '/' . $fileResourceModel->url
                        ], true),
                        'createdAt'    => time(),
                        'updatedAt'    => time(),
                        'createdByUid' => $fileResourceModel->createdByUid,
                        'updatedByUid' => $fileResourceModel->updatedByUid,
                    ];
                });
                $this->info(count($data));
                ResourcesModel::query()->insert($data);
            });
        dd(1);
    }
}
